package short_semester.questionnaire_planet.service.analyse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import short_semester.questionnaire_planet.dao.MultipleChoiceQuestionRecordDao;
import short_semester.questionnaire_planet.dao.QuestionnaireDao;
import short_semester.questionnaire_planet.dao.QuestionnaireRecordDao;
import short_semester.questionnaire_planet.dao.SingleChoiceQuestionRecordDao;
import short_semester.questionnaire_planet.pojo.*;
import short_semester.questionnaire_planet.request.analyse.CrossBody;
import short_semester.questionnaire_planet.response.AnalyseController.CrossResponse;
import short_semester.questionnaire_planet.service.editService.MultiChoiceQuestionService;
import short_semester.questionnaire_planet.service.editService.SingleChoiceQuestionService;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

@Service
public class CrossService {

    @Autowired
    SingleChoiceQuestionService singleChoiceQuestionService;
    @Autowired
    MultiChoiceQuestionService multiChoiceQuestionService;

    @Autowired
    SingleChoiceQuestionRecordDao singleChoiceQuestionRecordDao;
    @Autowired
    MultipleChoiceQuestionRecordDao multipleChoiceQuestionRecordDao;

    @Autowired
    SingleChoiceQuestionRecordService singleChoiceQuestionRecordService;
    @Autowired
    MultipleChoiceQuestionRecordService multipleChoiceQuestionRecordService;

    @Autowired
    QuestionnaireDao questionnaireDao;
    @Autowired
    QuestionnaireRecordDao questionnaireRecordDao;
    
    //交叉分析
    public CrossResponse cross(CrossBody body){
        CrossResponse response = new CrossResponse();
        Questionnaire questionnaire = questionnaireDao.getById(body.getQuestionnaireId());

        List<QuestionnaireRecord> recordList = questionnaireRecordDao.findByQuestionnaireAndIsSubmittedTrue(questionnaire);
        SingleChoiceQuestion singleChoiceQuestion_x = null;
        MultiChoiceQuestion multiChoiceQuestion_x = null;
        SingleChoiceQuestion singleChoiceQuestion_y = null;
        MultiChoiceQuestion multiChoiceQuestion_y = null;
        switch (body.getQuestionType_x()){
            case "radio":
                //问题x是单选
                singleChoiceQuestion_x = singleChoiceQuestionService.getQuestion(body.getQuestionId_x());

                switch (body.getQuestionType_y()){
                    case "radio":
                        //问题y是单选
                        singleChoiceQuestion_y = singleChoiceQuestionService.getQuestion(body.getQuestionId_y());
                        return crossResponse(singleChoiceQuestion_x,singleChoiceQuestion_y,recordList);
                    case "checkbox":
                        //问题y是多选
                        multiChoiceQuestion_y = multiChoiceQuestionService.getQuestion(body.getQuestionId_y());
                        return crossResponse(singleChoiceQuestion_x,multiChoiceQuestion_y,recordList);
                }
                break;
            case "checkbox":
                //问题x是多选
                multiChoiceQuestion_x = multiChoiceQuestionService.getQuestion(body.getQuestionId_x());

                switch (body.getQuestionType_y()){
                    case "radio":
                        //问题y是单选
                        singleChoiceQuestion_y = singleChoiceQuestionService.getQuestion(body.getQuestionId_y());
                        return crossResponse(multiChoiceQuestion_x,singleChoiceQuestion_y,recordList);
                    case "checkbox":
                        //问题y是多选
                        multiChoiceQuestion_y = multiChoiceQuestionService.getQuestion(body.getQuestionId_y());
                        return crossResponse(multiChoiceQuestion_x,multiChoiceQuestion_y,recordList);
                }
                break;
        }
        return null;
    }

    //单选x单选y
    public CrossResponse crossResponse(SingleChoiceQuestion question_x,SingleChoiceQuestion question_y,List<QuestionnaireRecord> recordList){
        CrossResponse response = new CrossResponse();
        response.setAmount_x(question_x.getCount());
        response.setAmount_y(question_y.getCount());
        int[][] arr = new int[question_y.getOptions().size()][question_x.getOptions().size()];
        response.setArr(arr);
        for (QuestionnaireRecord record : recordList) {
            SingleChoiceQuestionRecord record_x = singleChoiceQuestionRecordDao.findByQuestionnaireRecordAndSingleChoiceQuestion(record, question_x).get();
            SingleChoiceQuestionRecord record_y = singleChoiceQuestionRecordDao.findByQuestionnaireRecordAndSingleChoiceQuestion(record, question_y).get();

            int y=0;
            for (SingleChoiceOption option_y : question_y.getOptions()) {
                int x=0;
                for (SingleChoiceOption option_x: question_x.getOptions()){
                    if(Objects.equals(record_y.getAnswer(), option_y.getId())
                            && Objects.equals(record_x.getAnswer(), option_x.getId())){
                        ++arr[y][x];
                    }
                    ++x;
                }
                ++y;
            }
        }
        return response;
    }

    //单选x多选y
    public CrossResponse crossResponse(SingleChoiceQuestion question_x,MultiChoiceQuestion question_y,List<QuestionnaireRecord> recordList){
        CrossResponse response = new CrossResponse();
        response.setAmount_x(question_x.getCount());
        response.setAmount_y(question_y.getCount());
        int[][] arr = new int[question_y.getOptions().size()][question_x.getOptions().size()];
        response.setArr(arr);
        for (QuestionnaireRecord record : recordList) {
            SingleChoiceQuestionRecord record_x = singleChoiceQuestionRecordDao.findByQuestionnaireRecordAndSingleChoiceQuestion(record, question_x).get();
            MultiChoiceQuestionRecord record_y = multipleChoiceQuestionRecordDao.findByQuestionnaireRecordAndMultiChoiceQuestion(record, question_y).get();

            Set<Integer> multiAnswer_y = new TreeSet<>(record_y.getAnswer());
            int y=0;
            for (MultiChoiceOption option_y : question_y.getOptions()) {
                int x=0;
                for (SingleChoiceOption option_x: question_x.getOptions()){
                    if(multiAnswer_y.contains(option_y.getId())
                            && Objects.equals(record_x.getAnswer(), option_x.getId())){
                        ++arr[y][x];
                    }
                    ++x;
                }
                ++y;
            }
        }
        return response;
    }

    //多选x单选y
    public CrossResponse crossResponse(MultiChoiceQuestion question_x,SingleChoiceQuestion question_y,List<QuestionnaireRecord> recordList){
        CrossResponse response = new CrossResponse();
        response.setAmount_x(question_x.getCount());
        response.setAmount_y(question_y.getCount());
        int[][] arr = new int[question_y.getOptions().size()][question_x.getOptions().size()];
        response.setArr(arr);
        for (QuestionnaireRecord record : recordList) {
            MultiChoiceQuestionRecord record_x = multipleChoiceQuestionRecordDao.findByQuestionnaireRecordAndMultiChoiceQuestion(record, question_x).get();
            SingleChoiceQuestionRecord record_y = singleChoiceQuestionRecordDao.findByQuestionnaireRecordAndSingleChoiceQuestion(record, question_y).get();
            Set<Integer> multiAnswer_x = new TreeSet<>(record_x.getAnswer());
            int y=0;
            for (SingleChoiceOption option_y : question_y.getOptions()) {
                int x=0;
                for (MultiChoiceOption option_x: question_x.getOptions()){
                    if(Objects.equals(record_y.getAnswer(), option_y.getId())
                            && multiAnswer_x.contains(option_x.getId())){
                        ++arr[y][x];
                    }
                    ++x;
                }
                ++y;
            }
        }
        return response;
    }

    //多选x多选y
    public CrossResponse crossResponse(MultiChoiceQuestion question_x,MultiChoiceQuestion question_y,List<QuestionnaireRecord> recordList){
        CrossResponse response = new CrossResponse();
        response.setAmount_x(question_x.getCount());
        response.setAmount_y(question_y.getCount());
        int[][] arr = new int[question_y.getOptions().size()][question_x.getOptions().size()];
        response.setArr(arr);
        for (QuestionnaireRecord record : recordList) {
            MultiChoiceQuestionRecord record_x = multipleChoiceQuestionRecordDao.findByQuestionnaireRecordAndMultiChoiceQuestion(record, question_x).get();
            MultiChoiceQuestionRecord record_y = multipleChoiceQuestionRecordDao.findByQuestionnaireRecordAndMultiChoiceQuestion(record, question_y).get();
            Set<Integer> multiAnswer_x = new TreeSet<>(record_x.getAnswer());
            Set<Integer> multiAnswer_y = new TreeSet<>(record_y.getAnswer());
            int y=0;
            for (MultiChoiceOption option_y : question_y.getOptions()) {
                int x=0;
                for (MultiChoiceOption option_x: question_x.getOptions()){
                    if(multiAnswer_y.contains(option_y.getId())
                            && multiAnswer_x.contains(option_x.getId())){
                        ++arr[y][x];
                    }
                    ++x;
                }
                ++y;
            }
        }
        return response;
    }
}
